package com.finalhomework.dms.controller;

import com.finalhomework.dms.dao.AdminRepository;
import com.finalhomework.dms.entities.Admin;
import com.finalhomework.dms.entities.Dormitory;
import com.finalhomework.dms.entities.Students;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;


@Controller
public class AdminController {

    @Autowired
    AdminRepository adminRepository;

    @GetMapping("/adminAdd")
    public String addAdmin(Model model){
        List<Admin> all = adminRepository.findAll();
        model.addAttribute("allAdmin",all);
        Admin admin=new Admin();
        model.addAttribute("addAdmin",admin);
        return "admin/addAdmin";
    }

    @PostMapping("/admin/save")
    public String saveAdmin(Model model,Admin admin){
        adminRepository.save(admin);
        return "redirect:/adminAdd";

    }


    @GetMapping("/updateAdmin/{id}")
    public String toUpdateAdmin(@PathVariable("id")Integer id, Model model){
        Admin admin=adminRepository.getAdminByUid(id);
        model.addAttribute("ad",admin);
        List<Admin> all = adminRepository.findAll();
        model.addAttribute("allAdmin",all);


        return "admin/updateAdmin";

    }

    @PostMapping("/admin/update")
    public String updateStudent(Admin admin){
        System.out.println(admin);
        adminRepository.save(admin);
        return "redirect:/adminAdd";

    }

    @GetMapping("/delAdmin/{id}")
    public String deleteAdmin(@PathVariable("id")Integer id){
        //Admin admin=adminRepository.getAdminByUid(id);
        adminRepository.deleteById(id);
        return "redirect:/adminAdd";
    }
}
